<template>
  <div class="container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>添加学生</span>
          <el-button @click="goBack">返回列表</el-button>
        </div>
      </template>
      
      <el-form ref="formRef" :model="student" :rules="rules" label-width="120px" style="max-width: 600px;">
        <el-form-item label="学生姓名" prop="name">
          <el-input v-model="student.name" placeholder="请输入学生姓名" />
        </el-form-item>
        
        <el-form-item label="宿舍楼ID" prop="dormBuildId">
          <el-input-number v-model="student.dormBuildId" :min="1" placeholder="请输入宿舍楼ID" />
        </el-form-item>
        
        <el-form-item label="宿舍号" prop="dormName">
          <el-input v-model="student.dormName" placeholder="请输入宿舍号" />
        </el-form-item>
        
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="student.sex">
            <el-radio label="男">男</el-radio>
            <el-radio label="女">女</el-radio>
          </el-radio-group>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="submitForm">提交</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import axios from 'axios'
import { ElMessage } from 'element-plus'

export default {
  data() {
    return {
      student: {
        name: '',
        dormBuildId: null,
        dormName: '',
        sex: '男'
      },
      rules: {
        name: [
          { required: true, message: '请输入学生姓名', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        dormBuildId: [
          { required: true, message: '请选择宿舍楼ID', trigger: 'blur' }
        ],
        dormName: [
          { required: true, message: '请输入宿舍号', trigger: 'blur' }
        ],
        sex: [
          { required: true, message: '请选择性别', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    async submitForm() {
      this.$refs.formRef.validate(async (valid) => {
        if (valid) {
          try {
            const response = await axios.post(
              "http://localhost:8080/UserMgr_Web_exploded/students", 
              this.student,
              {
                headers: {
                  'Authorization': 'Bearer ' + localStorage.getItem('token')
                }
              }
            )
            
            if (response.data.success) {
              ElMessage.success('添加学生成功')
              this.$router.push('/students')
            } else {
              ElMessage.error(response.data.message || '添加失败')
            }
          } catch (error) {
            ElMessage.error('添加学生失败')
            console.error(error)
          }
        }
      })
    },
    resetForm() {
      this.$refs.formRef.resetFields()
    },
    goBack() {
      this.$router.push('/students')
    }
  }
}
</script>

<style scoped>
.container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}
</style>